package controllers

import (
	"WORKSPACE/models"
	"WORKSPACE/pkg/app"
	"WORKSPACE/pkg/e"
	"WORKSPACE/services"
	"encoding/json"
	"github.com/gin-gonic/gin"
)

func GetGoods(c *gin.Context){
	err, info, total := services.GetAllgoods()
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total},"查询成功")
}

func DelGoods(c *gin.Context){
	id := c.Query("goodsId")

	if id == ""{
		app.INFO(c,30001,"参数错误")
		return
	}
	err := services.Delgoods(id)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"删除成功")
}

func UpdateGoods(c *gin.Context){
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	goodsId := m["goodsId"]
	goodsName := m["goodsName"]
	gcId := m["gcId"]
	goodsNumber := m["goodsNumber"]
	goodsPrice := m["goodsPrice"]
	factoryId := m["factoryId"]


	if m["billId"] == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}

	err := services.Updategoods(models.Goods{Goods_id: goodsId, Goods_name: goodsName, Gc_id: gcId, Goods_number: goodsNumber, Goods_price: goodsPrice,Factory_id: factoryId})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"更新成功！")
}

func AddGoods(c *gin.Context){
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	goodsId := m["goodsId"]
	goodsName := m["goodsName"]
	gcId := m["gcId"]
	goodsNumber := m["goodsNumber"]
	goodsPrice := m["goodsPrice"]
	factoryId := m["factoryId"]

	if m["billId"] == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}

	err := services.Addgoods(models.Goods{Goods_id: goodsId, Goods_name: goodsName, Gc_id: gcId, Goods_number: goodsNumber, Goods_price: goodsPrice,Factory_id: factoryId})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"添加成功！")
}